package com.postek.printer;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.postek.sdk.CDFPTKAndroid;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class PostekPrinterModule extends UniModule {
    private static final String TAG = "PostekPrinterModule";
    private CDFPTKAndroid printerSDK;

    @Override
    public void onActivityCreate() {
        super.onActivityCreate();
        printerSDK = new CDFPTKAndroid();
    }

    @UniJSMethod(uiThread = true)
    public void connectNetworkPrinter(JSONObject options, UniJSCallback callback) {
        try {
            String ip = options.getString("ip");
            int port = options.getInteger("port");
            int timeout = options.getInteger("timeout");
            
            boolean result = printerSDK.PTK_SocketConnect(ip, port, timeout);
            
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", result);
                callback.invoke(data);
            }
        } catch (Exception e) {
            Log.e(TAG, "连接网络打印机失败: " + e.getMessage());
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", false);
                data.put("error", e.getMessage());
                callback.invoke(data);
            }
        }
    }

    @UniJSMethod(uiThread = true)
    public void printText(JSONObject options, UniJSCallback callback) {
        try {
            int x = options.getInteger("x");
            int y = options.getInteger("y");
            int direction = options.getInteger("direction");
            char font = (char) options.getInteger("font");
            int horizontal = options.getInteger("horizontal");
            int vertical = options.getInteger("vertical");
            char textType = (char) options.getInteger("textType");
            String content = options.getString("content");
            
            int result = printerSDK.PTK_DrawText(x, y, direction, font, horizontal, vertical, textType, content);
            
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", result == 0);
                data.put("code", result);
                callback.invoke(data);
            }
        } catch (Exception e) {
            Log.e(TAG, "打印文本失败: " + e.getMessage());
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", false);
                data.put("error", e.getMessage());
                callback.invoke(data);
            }
        }
    }

    @UniJSMethod(uiThread = true)
    public void printLabel(JSONObject options, UniJSCallback callback) {
        try {
            int number = options.getInteger("number");
            int copies = options.getInteger("copies");
            
            int result = printerSDK.PTK_PrintLabel(number, copies);
            
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", result == 0);
                data.put("code", result);
                callback.invoke(data);
            }
        } catch (Exception e) {
            Log.e(TAG, "打印标签失败: " + e.getMessage());
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", false);
                data.put("error", e.getMessage());
                callback.invoke(data);
            }
        }
    }

    @UniJSMethod(uiThread = true)
    public void disconnectPrinter(UniJSCallback callback) {
        try {
            printerSDK.PTK_SocketClose();
            
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", true);
                callback.invoke(data);
            }
        } catch (Exception e) {
            Log.e(TAG, "断开打印机连接失败: " + e.getMessage());
            if (callback != null) {
                JSONObject data = new JSONObject();
                data.put("success", false);
                data.put("error", e.getMessage());
                callback.invoke(data);
            }
        }
    }

    // 可以继续添加其他API方法...
}